package com.zx.znav.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zx.znav.bean.Link;
import com.zx.znav.bean.LinkStar;
import com.zx.znav.bean.LinkTag;
import com.zx.znav.bean.Tag;
import com.zx.znav.constant.Constants;
import com.zx.znav.controller.vo.UserVO;
import com.zx.znav.response.CommonReturnType;
import com.zx.znav.service.LinkService;
import com.zx.znav.service.LinkStarService;
import com.zx.znav.service.LinkTagService;
import com.zx.znav.service.TagService;
import com.zx.znav.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 链接详情
 * @since 2023-07-24 20:09
 */
@Controller
@RequestMapping("/link")
public class ShowLinkController {
    @Autowired
    private LinkService linkService;
    @Autowired
    private TagService tagService;
    @Autowired
    private LinkTagService linkTagService;
    @Autowired
    private LinkStarService linkStarService;

    @GetMapping("/{linkId}")
    public String index(@PathVariable Integer linkId, Model model) {
        //将viewCnt + 1
        linkService.increaseViewCnt(linkId);
        //查链接
        Link link = linkService.getById(linkId);
        model.addAttribute(Constants.LinK_KEY, link);
        //查标签
        List<LinkTag> linkTagList = linkTagService.listByLinkId(linkId);
        if (linkTagList.size() > 0) {
            List<Tag> tags = tagService.listByIds(linkTagList.stream().map(LinkTag::getTagId).collect(Collectors.toList()));
            model.addAttribute(Constants.TAG_KEY, tags);
        }
        //查热门标签
        List<Tag> hotTags = tagService.listTopTag(Constants.SHOW_HOT_TAG_SIZE);
        model.addAttribute(Constants.HOT_TAGS, hotTags);
        //查相关链接
        List<Link> relatedLinks = linkService.listNLinkBySecondNavId(link.getSecondNavId(), Constants.SHOW_RELATED_LINK_SIZE);
        //去除自己
        relatedLinks = relatedLinks.stream().filter(e -> !Objects.equals(e.getId(), linkId)).collect(Collectors.toList());
        model.addAttribute(Constants.RELATED_LINKS, relatedLinks);

        //收藏
        model.addAttribute("star", false);
        return "link-detail";
    }

    /**
     * 收藏/取消收藏 链接
     *
     * @param linkId 链接id
     * @param star   true:收藏, false: 取消收藏
     */
    @PostMapping("/star")
    @ResponseBody
    public Object star(Integer linkId, Boolean star) {
        if (UserHolder.getUser() == null) {
            return CommonReturnType.create("用户未登录");
        }
        Integer userId = UserHolder.getUser().getId();
        if (star) {
            if(linkStarService.isStar(linkId, userId)){
                return CommonReturnType.create("已经收藏");
            }
            linkStarService.star(linkId, userId);
        } else {
            linkStarService.unstar(linkId, userId);
        }
        return CommonReturnType.create("操作成功");
    }

    //加载收藏页面片段
    @GetMapping("/starContainer")
    public String starContainer(Model model, Integer linkId){
        //查链接
        Link link = linkService.getById(linkId);
        model.addAttribute(Constants.LinK_KEY, link);
        UserVO userVO = UserHolder.getUser();
        model.addAttribute(Constants.MODEL_USER_KEY, userVO);
        boolean star = false;
        if(userVO != null){
            star = linkStarService.isStar(linkId, userVO.getId());
        }
        model.addAttribute("star", star);
        return "link-detail :: star";
    }
}
